#!/bin/bash
FILES=`ls -1 makefile.d`

echo -n "all: checkhg " > Makefile
for file in $FILES; do
  echo -n "$file " >> Makefile
done
echo "" >> Makefile
cat << _EOF_ >> Makefile
	@echo '***' tada.wav

install: all
	# do stuff

checkhg:
	@if [ -z "\`hg --quiet incoming\`" ] ; then : ; else echo please hg pull -u ; exit 1 ; fi

clean:
	@echo '<=>' clean
	@rm -rf tree work
_EOF_
OWNER="root:root"
cat tree.rc | while read line; do
  isowner=`echo "$line" | cut -f1 | sed -e "s/.*:.*/yes/"`
  if [ "$isowner" = "yes" ]; then
    user=`echo "$line" | cut -f1`
    if [ ! -z "$user" ]; then
      OWNER="$user"
    fi
  else
    mode=`echo "$line" | cut -f1`
	dirname=`echo "$line" | cut -f2`
    if [ ! -z "$dirname" ]; then
      echo "	@mkdir -p $dirname" >> Makefile
	  echo "	@chown $OWNER $dirname" >> Makefile
	  echo "	@chmod $mode $dirname" >> Makefile
    fi
  fi
done
echo "" >> Makefile
for file in makefile.d/*; do
  cat $file >> Makefile
  echo "" >> Makefile
done
